/*
  © 2022 BLACK SESAME TECHNOLOGIES
 All rights reserved.
 (This product is licensed to: [Hanhai-ADSP Autonomous Driving Solution Platform]
 This computer software is protected by copyright law and international treaties.
 Use, reproduction, or distribution of this software is subject to the license agreement between you
 and Black Sesame Technologies. Unauthorized reproduction or distribution of this program, or any
 portion of it, may result in severe civil and criminal penalties and will be prosecuted to the
 maximum extent possible under the law.
*/

#include <gtest/gtest.h>
#include "dal/domain_participant.h"
#include "dal/publisher.h"
#include "dal/topic.h"
#include "dal_greenstonedds/converter/QosConverter.hpp"
#include "data_type/HelloWorldTopicDataType.h"

TEST(pub_create_and_delete, test_publisher)
{
    dal_ret_t ret;
    dal_domain_participant_listener_callbacks_t participant_callbacks = { };
    dal_domain_participant_listener_t* participant_listener =
        dal_create_domain_participant_listener(participant_callbacks);
    dal_domain_participant_t* participant = dal_create_domain_participant(
        20, DAL_PARTICIPANT_QOS_DEFAULT, participant_listener, DAL_ANY_STATUS);

    dal_publisher_listener_callbacks_t pub_callbacks = { };
    dal_publisher_listener_t* pub_listener = dal_create_publisher_listener(pub_callbacks);
    dal_publisher_t* pub = dal_domain_participant_create_publisher(
        participant, DAL_PUBLISHER_QOS_DEFAULT, pub_listener, DAL_ANY_STATUS);
    EXPECT_NE(pub->handle_, nullptr);

    ret = dal_delete_publisher_listener(pub_listener);
    EXPECT_EQ(ret, DAL_RETCODE_PRECONDITION_NOT_MET);
    ret = dal_domain_participant_delete_publisher(participant, pub);
    EXPECT_EQ(ret, DAL_RETCODE_OK);
    ret = dal_delete_publisher_listener(pub_listener);
    EXPECT_EQ(ret, DAL_RETCODE_OK);

    dal_delete_domain_participant(participant);
    ret = dal_delete_domain_participant_listener(participant_listener);
    EXPECT_EQ(ret, DAL_RETCODE_OK);
}

TEST(datawriter_create_and_delete, test_publisher)
{
    dal_ret_t ret;
    dal_domain_participant_listener_callbacks_t participant_callbacks = { };
    dal_domain_participant_listener_t* participant_listener =
        dal_create_domain_participant_listener(participant_callbacks);
    dal_domain_participant_t* participant = dal_create_domain_participant(
        20, DAL_PARTICIPANT_QOS_DEFAULT, participant_listener, DAL_ANY_STATUS);

    dal_publisher_listener_callbacks_t pub_callbacks = { };
    dal_publisher_listener_t* pub_listener = dal_create_publisher_listener(pub_callbacks);
    dal_publisher_t* pub = dal_domain_participant_create_publisher(
        participant, DAL_PUBLISHER_QOS_DEFAULT, pub_listener, DAL_ANY_STATUS);

    dal_type_support_t* ts = dal_create_type_support(new HelloWorldTopicDataType());
    ret = dal_type_support_register_type(ts, participant, dal_type_support_get_type_name(ts));

    dal_topic_listener_callbacks_t topic_callbacks = { };
    dal_topic_listener_t* topic_listener = dal_create_topic_listener(topic_callbacks);
    dal_topic_t* topic = dal_domain_participant_create_topic(participant,
                                                             "HelloWorldTopic",
                                                             dal_type_support_get_type_name(ts),
                                                             DAL_TOPIC_QOS_DEFAULT,
                                                             topic_listener,
                                                             DAL_ANY_STATUS);

    dal_datawriter_listener_callbacks_t writer_callbacks = { };
    dal_datawriter_listener_t* writer_listener = dal_create_datawriter_listener(writer_callbacks);
    dal_datawriter_t* writer = dal_publisher_create_datawriter(
        pub, topic, DAL_DATAWRITER_QOS_DEFAULT, writer_listener, DAL_ANY_STATUS);
    EXPECT_NE(writer->handle_, nullptr);

    ret = dal_delete_datawriter_listener(writer_listener);
    EXPECT_EQ(ret, DAL_RETCODE_PRECONDITION_NOT_MET);
    ret = dal_publisher_delete_datawriter(pub, writer);
    EXPECT_EQ(ret, DAL_RETCODE_OK);
    ret = dal_delete_datawriter_listener(writer_listener);
    EXPECT_EQ(ret, DAL_RETCODE_OK);

    dal_domain_participant_delete_topic(participant, topic);
    dal_delete_topic_listener(topic_listener);
    dal_delete_type_support(ts);

    dal_domain_participant_delete_publisher(participant, pub);
    dal_delete_publisher_listener(pub_listener);

    dal_delete_domain_participant(participant);
    dal_delete_domain_participant_listener(participant_listener);
}